/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.xtecuan.modelo.ejb;

import com.sun.xml.ws.tx.at.v10.types.PrepareResponse;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
import org.xtecuan.modelo.dto.UsuariosDTO;

/**
 *
 * @author ernesto
 */
@Stateless
public class UsuarioEJB {

    Logger logger = Logger.getLogger(UsuarioEJB.class);
    @Resource(name = "datasources")
    private DataSource datasources;

    public Boolean loginEJB(UsuariosDTO usuario) {

        Connection conn = null;
        PreparedStatement psta = null;
        boolean resultado = false;

        try {

            if (usuario.getLogin() != null && usuario.getLogin().length() > 0 && usuario.getPasswd() != null && usuario.getPasswd().length() > 0) {
                conn = datasources.getConnection();
                psta = conn.prepareStatement(UsuariosDTO.getLOGIN());
                psta.setString(1, usuario.getLogin());
                psta.setString(2, usuario.getPasswd());

                ResultSet rset = psta.executeQuery();

                if (rset.next()) {
                    resultado = true;
                    logger.info("Consulta hecha");
                }
                
                rset.close();
                psta.close();
            }
        } catch (Exception e) {
            logger.error("Error al logearse: ", e);
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e) {
                logger.error("Error al cerrar la conexion: ", e);
            }
        }

        return resultado;
    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
}
